package myServlet;

import Smt.ServerImp.SmtUserServerImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@SuppressWarnings("serial")
public class Login extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String flag = request.getParameter("num");
		SmtUserServerImp susi = new SmtUserServerImp();
		if (flag == null) {
			response.sendRedirect("Login.jsp");
		} else if (flag.equals("1")) {
			if (susi.login(name, password)) {
				HttpSession session = request.getSession();
				if (susi.rms(name)) {
					session.setAttribute("admin", "admin");
				}
				session.setAttribute("loginname", name);
				response.sendRedirect("Index.jsp");
			} else {
				request.setAttribute("flag", "用户名或密码错误!");
				request.getRequestDispatcher("Login.jsp").forward(request, response);
			}
		} else {
			response.sendRedirect("Login.jsp");
		}
	}
}
